<?php
/**
 * Created by PhpStorm.
 * User: goouc
 * Date: 2019/9/27
 * Time: 10:11
 */
global $_W,$_GPC;
$op = empty($_GPC['op'])?'category':$_GPC['op'];
$oop = empty($_GPC['oop'])?'list':$_GPC['oop'];
$paixu = empty($_GPC['paixu']) ? 1 : $_GPC['paixu'];
//文章分类
if($op == 'category'){
    if ($paixu == 1) {
        $order = " ORDER BY `order` DESC";
    } elseif ($paixu == 2) {
        $order = " ORDER BY `order` ASC";
    }
    $list = pdo_fetchall("select * from ".tablename($this->t_category)." where weid = :weid ".$order,array(':weid'=>$_W['uniacid']));
    $num = pdo_fetchcolumn("select COUNT(*) from ".tablename($this->t_category)." where weid = :weid and status = :status ",[':weid'=>$_W['uniacid'],':status'=>1]);
    if($oop == 'add'){
        if($_W['isajax']){
            $cid = $_GPC['cid'];
            $info = pdo_get($this->t_category,['id'=>$cid]);
            echo json_encode($info);exit;
        }
        if(checksubmit('token')){
            $data['weid'] = $_W['uniacid'];
            $data['name'] = $_GPC['name'];
            $data['order'] = $_GPC['order'] > 99 ? 99 :$_GPC['order'];
            $data['status'] = $_GPC['status'];
            $data['createtime'] = time();

            if(empty($_GPC['categoryid'])){
                $res = pdo_insert($this->t_category,$data);
            }else{
                $res = pdo_update($this->t_category,$data,array('id'=>$_GPC['categoryid']));
            }
            if($res){
                message('操作成功',$this->createWebUrl('consultation'),'succss');
            }else{
                message('操作失败',$this->createWebUrl('consultation'),'error');
            }
        }
    }elseif($oop == 'delete'){
        $cid = $_GPC['cid'];
        $res = pdo_delete($this->t_category,['id'=>$cid]);
        if($res){
            message('操作成功',$this->createWebUrl('consultation'),'succss');
        }else{
            message('操作失败',$this->createWebUrl('consultation'),'error');
        }
    }elseif($oop == 'changestatus'){
        $cid = $_GPC['cid'];
        $data['status'] = $_GPC['sta'];
        //先判断要修改状态是1 还是2
        if($data['status'] == 1){
            //判断显示的分类有几个
            $num = pdo_fetchcolumn("select COUNT(*) from ".tablename($this->t_category)." where weid = :weid and status = :status ",[':weid'=>$_W['uniacid'],':status'=>1]);
            if($num >= 5){
                $message = ['success'=>2,'msg'=>'分类已达最大到上限！请修改其他分类状态后再显示次分类！'];
                echo json_encode($message);exit;
            }
        }
        $res = pdo_update($this->t_category,$data,['id'=>$cid]);
        if($res){
            $message = ['success'=>1,'修改成功'];
        }else{
            $message = ['success'=>0,'msg'=>'修改失败'];
        }
        echo json_encode($message);exit;
    }elseif ($oop == 'sort') {
        if ($this->userid == $_W['uid']) {
            $message = array('success' => 0, 'mess' => $this->msg);
            echo json_encode($message);
            exit;
        }
        $id = $_GPC['cid'];
        $data['order'] = $_GPC['order'];
        $res = pdo_update($this->t_category, $data, array('id' => $id, 'weid' => $_W['uniacid']));
        if ($res) {
            $message = array('success' => 1, 'mess' => '修改成功');
        } else {
            $message = array('success' => 0, 'mess' => '修改失败');
        }
    }
}elseif($op == 'article'){
    $pindex = max(1,$_GPC['page']);
    $psize = 5;
    //文章管理
    if ($paixu == 1) {
        $order = " ORDER BY a.order DESC ";
    } elseif ($paixu == 2) {
        $order = " ORDER BY a.order ASC ";
    }
    $list = pdo_fetchall("select a.id,a.title,c.name as cname,a.image,a.status,a.recommond,a.order from ".tablename($this->t_article)." a JOIN ".tablename($this->t_category)." c ON a.cid = c.id where a.weid = :weid ".$order." LIMIT ".($pindex - 1)*$psize.','.$psize,[':weid'=>$_W['uniacid']]);

    $total = pdo_fetchcolumn("select COUNT(*) from ".tablename($this->t_article)." where weid = :weid  ",[':weid'=>$_W['uniacid']]);

    $page = pagination($total,$pindex,$psize);

}elseif ($op == 'sort') {
    if ($this->userid == $_W['uid']) {
        $message = array('success' => 0, 'mess' => $this->msg);
        echo json_encode($message);
        exit;
    }
    $id = $_GPC['aid'];
    $data['order'] = $_GPC['order'];

    $res = pdo_update($this->t_article, $data, array('id' => $id, 'weid' => $_W['uniacid']));
    if ($res) {
        $message = array('success' => 1, 'mess' => '修改成功');
    } else {
        $message = array('success' => 0, 'mess' => '修改失败');
    }
}elseif($op == 'createarticle'){
    $articleid = $_GPC['articleid'];
    $info = pdo_get($this->t_article,['id'=>$articleid]);
    $categorylist = pdo_getall($this->t_category,['weid'=>$_W['uniacid']]);
    if(checksubmit('token')){
        $articleid = $_GPC['articleid'];
        $data = [
            'weid'=>$_W['uniacid'],
            'title'=>$_GPC['title'],
            'image'=>$_GPC['image'],
            'content'=>$_GPC['content'],
            'status'=>$_GPC['status'],
            'order'=>$_GPC['order'],
            'cid'=>$_GPC['cid'],
            'createtime'=>time()
        ];
        if(empty($articleid)){
            $res = pdo_insert($this->t_article,$data);
        }else{
            $res = pdo_update($this->t_article,$data,['id'=>$articleid]);
        }
        if($res){
            message('操作成功',$this->createWebUrl('consultation',['op'=>'article']),'succss');
        }else{
            message('操作失败',$this->createWebUrl('consultation',['op'=>'article']),'error');
        }
    }
}elseif($op == 'deletearticle'){
    $articleid = $_GPC['articleid'];
    $res = pdo_delete($this->t_article,['id'=>$articleid]);
    if($res){
        message('操作成功',$this->createWebUrl('consultation',['op'=>'article']),'succss');
    }else{
        message('操作失败',$this->createWebUrl('consultation',['op'=>'article']),'error');
    }
}elseif($op == 'changestatus'){
    $aid = $_GPC['aid'];
    $data['status'] = $_GPC['sta'];
    $res = pdo_update($this->t_article,$data,['id'=>$aid]);
    if($res){
        $message = ['success'=>1,'修改成功'];
    }else{
        $message = ['success'=>0,'修改失败'];
    }
    echo json_encode($message);exit;
}elseif($op == 'recommend'){
    $aid = $_GPC['aid'];
    $data['recommond'] = $_GPC['sta'];
    $res = pdo_update($this->t_article,$data,['id'=>$aid]);
    if($res){
        $message = ['success'=>1,'修改成功'];
    }else{
        $message = ['success'=>0,'修改失败'];
    }
    echo json_encode($message);exit;
}
include $this->template('web/consultation');